package gui.component;

import java.util.LinkedList;
import java.util.concurrent.Callable;
import javax.swing.BoxLayout;
import javax.swing.JPanel;

public class WorkProgress extends JPanel
{        
    LinkedList<Work> works;
    public static boolean someOneIn;
    static int totalID;
    
    public WorkProgress()
    {
        super();
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        works = new LinkedList<>();   
        totalID=0;
    }
    
    public Work getWork(int workID)
    {
        Work result=null;
        for (Work work : works) 
        {
            if (work.workID==workID)
            {
                result= work;
                break;
            }
        }
        return result;
    }
    
    public void addWork(Callable<WorkStatus> method, String description)
    {
        totalID++;
        Work w = new Work(method, description, totalID);
        this.add(w);
        works.add(w);       
    }
    
    public void start() throws Exception
    {
        new Thread(() -> {            
                works.stream().forEach((work) -> 
                {
                    try
                    {                    
                        work.start();
                    }
                    catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }
                });               
        }).start();
    }
    
    public String generateReport()
    {
        String report="";
        report = works.stream().filter((work) -> (work.result!=null)).filter((work) -> (work.result.status==WorkStatus.Status.FAIL)).map((work) -> "- "+work.result.message+"\n").reduce(report, String::concat);
        return report;
    }
}
